$(function () {
    $("#jqGrid").jqGrid({
        url: '../webantmanager/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
			{ label: '爬虫名称', name: 'name', index: 'name', width: 80 ,formatter:function(cellvalue, options, rowObject){
				
				return cellvalue.replace("xyz.yuanjilu.pageprocessor.","");
				
			}}, 			
			{ label: '爬虫描述', name: 'remarks', index: 'remarks', width: 180,formatter:function(cellvalue, options, rowObject){
				var temp = "<a href='"+rowObject.initTargetUrl+"' target='_blank'>"+cellvalue+" </a>";
				return temp;
			} }, 			
			{ label: '爬虫状态', name: 'status', index: 'status', width: 50,formatter:function(cellvalue, options, rowObject){
				if(cellvalue=="Init"){
					return '<span class="label label-success">初始化</span>';
				}
				if(cellvalue=="Running"){
					return '<span class="label label-success">运行中</span>';
				}
				if(cellvalue=="Stopped"){
					return '<span class="label label-danger">已结束</span>';
				}
				return cellvalue;
				
			}},
			{ label: '定时任务状态', name: 'scheduleJobStatus', index: 'scheduleJobStatus', width: 150,formatter:function(cellvalue, options, rowObject){
				if(cellvalue=="0"){
					return '<span class="label label-success">正常</span>下次执行时间：'+rowObject.scheduleJobNextRunTime;
				}
				if(cellvalue=="1"){
					return '<span class="label label-danger">暂停</span>';
				}
				
				return '<span class="label label-danger">未添加定时任务</span>';
				
			}},
			{ label: '关联网站', name: 'wetSiteName', index: 'wetSiteName', width: 80 },
        ],
		viewrecords: true,
        height: 600,
        rowNum: 50,
		rowList : [50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		antinfo: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.antinfo = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.antinfo.id == null ? "../webantmanager/save" : "../webantmanager/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.antinfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../webantmanager/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		start: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要启动选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../webantmanager/start",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		stop: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要启动选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../webantmanager/stop",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		addScheduleJob: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要启动选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../webantmanager/addScheduleJob",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		startScheduleJob: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要启动选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../webantmanager/startScheduleJob",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		stopScheduleJob: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要启动选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../webantmanager/stopScheduleJob",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		selectweb: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			T.selectWebSite(function(selectWebId){
				var param={ids:ids,selectWebId:selectWebId[0]}
				$.ajax({
					type: "POST",
				    url: "../webantmanager/bindwebsite",
				    contentType: "application/json",
				    data:JSON.stringify(param),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
				
			},{selectType:'radio'});
		},
		getInfo: function(id){
			$.get("../webantmanager/info/"+id, function(r){
                vm.antinfo = r.antinfo;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});